﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.IO;
using ATW_018;
using System.Diagnostics;
using HalconDotNet;
using System.Threading;

namespace CsThreadUsingTask
{
    public partial class Form1 : Form
    {
        bool flag = false;
        public Form1()
        {
            InitializeComponent();

        }

          private void button1_Click(object sender, EventArgs e)
          {

              flag = true;
              Stopwatch watch = new Stopwatch();
              Task[] tasks = new Task[2];
              Queue<AreaDetect_CS> TaskQueue = new Queue<AreaDetect_CS>();
               tasks[0] = Task.Factory.StartNew(() =>
              {
                  //thread1

                  Console.WriteLine("task1 is running!");
                  DirectoryInfo TheFolder = new DirectoryInfo("E:/Halcon/标定板");
                  foreach (FileInfo NextFile in TheFolder.GetFiles())  {
                      watch.Start();
                    AreaDetect_CS areadetect = new AreaDetect_CS();
                      watch.Stop();
                      TimeSpan time = watch.Elapsed;
                     double timer = time.TotalMilliseconds;
                     Console.WriteLine(NextFile.FullName);
                      areadetect.LoadImg(hWindowControl1.HalconWindow, NextFile.FullName);
                      areadetect.DetecInit(hWindowControl1.HalconWindow);
                      TaskQueue.Enqueue(areadetect);

                      // Thread.Sleep(2000); 
                  }

              });
               tasks[1] = Task.Factory.StartNew(() =>
              {
                  Console.WriteLine("task2 is running!");
                  watch.Start();
                  while (flag)
                  {
                      if (TaskQueue.Count > 0)
                      {
                          TimeSpan time0 = watch.Elapsed;
                          AreaDetect_CS temp = new AreaDetect_CS();
                          temp = TaskQueue.Dequeue();
                          temp.ReadParameter();
                          temp.GetLineEdgePt();
                          temp.GetCornerPt();
                          temp.GetConerAngle();
                          temp.GetDiagonal();
                          temp.GetArcEdgePt();
                          temp.GetArcEdgePtToCricle();
                          temp.CalMonocrystalParamater();
                          temp.GetMonocrystalROI();
                          temp.HoleDetect();
                          // string str=areadetect.ReturnArcLength1();
                          watch.Stop();
                          TimeSpan time = watch.Elapsed;
                          TimeSpan timer = time - time0;
                      Console.WriteLine("result="+ temp.ReturnDiagonalAngle()+"    "+ time.TotalMilliseconds +"ms");

                  }
  //                     if (TaskQueue.Count <= 0)
  //                     {
  //                         Thread.Sleep(20);
  //                     }
                  }

              });
  //             //等待延迟，超过5S报错
  //             Task.WaitAll(tasks, 5000);
  //             for (int i = 0; i < tasks.Length; i++)
  //             {
  //                 if (tasks[i].Status != TaskStatus.RanToCompletion)
  //                 {
  //                     Console.WriteLine("Task {0} Error!", i + 1);
  //                 }
  //             }
  //             Console.Read();
          }
      }
      
//         private void button1_Click(object sender, EventArgs e)
//         {
//             Task[] Tasks = new Task[2];
//             Queue<HObject> TaskQueue = new Queue<HObject>();
//             AreaDetect_CS areadetect=new AreaDetect_CS();
//             HObject img;
//             HOperatorSet.GenEmptyObj(out img);
//             Tasks[0] = Task.Factory.StartNew(() => {
//             DirectoryInfo TheFolder = new DirectoryInfo("E:/Halcon/标定板");
//                 flag = true;
//                 foreach (FileInfo NextFile in TheFolder.GetFiles())
//                 {
//                     HOperatorSet.ReadImage(out img, NextFile.FullName);
//                     TaskQueue.Enqueue(img);
//                   //  img.Dispose();
//                     Console.WriteLine(NextFile.FullName);
//                 }
//             });
//             Tasks[1] = Task.Factory.StartNew(() => {
//                 Console.WriteLine("task2 is running!");
//                 while (flag) {
//                     if (TaskQueue.Count > 0)
//                     {
//                         HObject temp;
//                         HOperatorSet.GenEmptyObj(out temp);
//                         temp = TaskQueue.Dequeue();
//                         areadetect.GetImg(hWindowControl1.HalconWindow, temp);
//                         areadetect.ReadParameter();
//                         areadetect.GetLineEdgePt();
//                         areadetect.GetCornerPt();
//                         areadetect.GetConerAngle();
//                         areadetect.GetDiagonal();
//                         areadetect.GetArcEdgePt();
//                         areadetect.GetArcEdgePtToCricle();
//                         areadetect.CalMonocrystalParamater();
//                         areadetect.GetMonocrystalROI();
//                         areadetect.HoleDetect();
//                         Console.WriteLine(areadetect.ReturnDiagonalAngle());
// 
//                     }
//                     else
//                         Thread.Sleep(1);
//                    
//                 }
//             });
//         }
//       }
 }
